package com.yupi.springbootinit.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.exception.ThrowUtils;
import com.yupi.springbootinit.model.entity.Picture;
import com.yupi.springbootinit.service.PictureService;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 图片服务类
 */

@Service
@Slf4j
public class PictureServiceImpl implements PictureService {


    @Override
    public Page<Picture> searchPicture(String searchText, long pageSize, long pageNum) {
        if(searchText == null || "".equals(searchText)){
            searchText = "夏目友人帐";
        }
        long current = (pageNum -1)*pageSize;
        String url = String.format("https://cn.bing.com/images/search?q=%s&first=%s",searchText,current);
        Document doc = null;
        Document document = null;
        try {
            doc = Jsoup.connect(url).get();
        } catch (IOException e) {
            throw  new BusinessException(ErrorCode.SYSTEM_ERROR,"数据获取异常");
        }
        Elements select = doc.select("#mmComponent_images_1");
        List<Picture> list = new ArrayList<>();
        for (Element element : select) {
            List<Node> nodes = element.childNodes();//获取到ul
            for (Node node:nodes){
                List<Node> nodes1 = node.childNodes();//获取到li
                for (Node nodeTmp1 : nodes1){
                    try{
                        Node node1 = nodeTmp1.childNode(0).childNode(0).childNode(0);
                        Attributes attributes = node1.attributes();
                        String m = attributes.get("m");
                        Map<String,String> map = JSONUtil.toBean(m, Map.class);
                        String murl = map.get("murl");
//                        Connection.Response response = Jsoup.connect(murl).ignoreContentType(true).execute();
//                        int statusCode = response.statusCode();
////                        if (statusCode >= 400 && statusCode <= 599) {
//                        if (!(statusCode==200)) {
//                            log.info("Image cannot be accessed. HTTP response code: " + statusCode);
//                            continue;
//                        }
                        String title = map.get("t");
                        System.out.println("murl："+murl + " ;title:"+title);
                        Picture picture = new Picture();
                        picture.setTitle(title);picture.setUrl(murl);
                        list.add(picture);
                        if(list.size() >= pageSize){
                            Page<Picture> picturePage = new Page<>(pageNum,pageSize);
                            picturePage.setRecords(list);
                            return picturePage;
                        }
                    }catch (Exception e){
                        continue;
                    }
                }
            }
        }
        Page<Picture> picturePage = new Page<>(pageNum,pageSize);
        picturePage.setRecords(list);
        return picturePage;
    }
}
